package com.example.taobaou.ui.fragment;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.taobaou.R;
import com.example.taobaou.base.BaseFragment;
import com.example.taobaou.model.domain.Categories;
import com.example.taobaou.presenter.impl.HomePresenterImpl;
import com.example.taobaou.ui.activity.IMainActivity;
import com.example.taobaou.ui.activity.MainActivity;
import com.example.taobaou.ui.activity.ScanQrCodeActivity;
import com.example.taobaou.ui.adapter.HomePagerAdapter;
import com.example.taobaou.utils.PresenterManager;
import com.example.taobaou.view.IHomeCallback;
import com.google.android.material.tabs.TabLayout;
import com.vondear.rxfeature.activity.ActivityScanerCode;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements IHomeCallback {


    private HomePresenterImpl homePresenterImol;
    TabLayout tabLayout;
    ViewPager viewPager;
    private HomePagerAdapter adapter;
    private View goSearch;
    private View scan;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    protected int getRootViewResID() {
        return R.layout.fragment_home;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_home_fragment_layout, container, false);
    }

    @Override
    protected void onRetryClick() {
        //点击重试
        if (homePresenterImol != null) {
            homePresenterImol.getCategories();
        }
    }

    @Override
    protected void initView(View rootView) {
        tabLayout = rootView.findViewById(R.id.home_indicator);
        viewPager = rootView.findViewById(R.id.home_pager);

        tabLayout.setupWithViewPager(viewPager);
        adapter = new HomePagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);

        goSearch = rootView.findViewById(R.id.go_search);

        scan = rootView.findViewById(R.id.scan_icon);
    }

    @Override
    protected void initListener() {
        goSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentActivity activity = getActivity();
                if (activity instanceof IMainActivity) {
                    ((IMainActivity) activity).goSearch();
                }
            }
        });

        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goTo(ScanQrCodeActivity.class);
            }
        });
    }

    @Override
    protected void loadData() {
        //加载数据
        homePresenterImol.getCategories();
    }

    @Override
    protected void initPresenter() {
        //创建Presenter
        homePresenterImol = PresenterManager.getInstance().getHomePresenterImol();
        homePresenterImol.registerViewCallback(this);
    }

    @Override
    public void onCategoriesLoaded(Categories category) {
        setState(State.SUCCESS);
        //加载的数据会从这里回来
        if (adapter != null) {
            adapter.setCategories(category);
        }
    }

    @Override
    public void onNatworkError() {
        setState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setState(State.EMPTY);
    }

    @Override
    protected void release() {
        //取消回调注册
        if (homePresenterImol != null) {
            homePresenterImol.unregisiterViewCallback(this);
        }
    }
}
